package com.company.链表;

/**
 * @author zhangjian
 * @data 2023/2/17
 * @Version 1.0
 */
public class 单链表 {
    public static void main(String[] args) {
        testNode first = new testNode(10, 3);
        testNode second = new testNode(10, 4);
        testNode third = new testNode(8, 2);
        SingleLinkedList1 test = new SingleLinkedList1();
        test.add(first);
        test.add(second);
        test.add(third);


        /*int b = test.checktestNode(10);
        System.out.println(b);
        test.deltestNode(10);*/
        test.show();
    }
}

class testNode {
    public int priority;
    public int index;
    public testNode next;

    public testNode(int priority, int index) {
        this.priority = priority;
        this.index = index;
    }

    @Override
    public String toString() {
        return "testNode{" +
                "priority=" + priority +
                ", index='" + index + '\'' +
                ", next=" + next +
                '}';
    }
}

class SingleLinkedList1 {

    //先初始化一个头节点，头节点不能动，否则会导致整个链表丢失
    private testNode head = new testNode(0, 0);

    public void add(testNode node) {
        testNode temp = head;
        while (true) {
            if (temp.next != null) {
                temp = temp.next;
            } else {
                temp.next = node;
                break;
            }
        }
    }

    //删除链表中元素
    //传入参数为想要删除的节点的data域，是为了匹配到想要删除的节点
    public void deltestNode(int priority) {
        testNode temp = head;
        if (temp.next != null) {
            while (true) {
                if (temp.next.priority == priority) {
                    //条件匹配，找到想要删除的节点
                    temp.next = temp.next.next;
                    break;
                } else {
                    temp = temp.next;
                }
            }
        } else {
            System.out.println("链表为空");
        }
    }

    public void show() {
        testNode temp;
        temp = head;
        if (temp.next != null) {
            while (true) {
                if (temp.next == null) {
                    break;
                } else {
                    System.out.println(temp);
                    temp = temp.next;
                }
            }
        } else {
            System.out.println("链表为空");
        }


    }

    public int checktestNode(int priority) {
        testNode temp;
        temp = head;
        while (true) {
            if (temp.next.priority == priority) {
                return temp.next.index;
            } else {
                temp = temp.next;
                //与deltestNode()方法相同道理
                if (temp.next == null) {
                    return 0;

                }
            }
        }
    }
}